package com.tianjs.wechat.impl;

import java.io.IOException;

import org.apache.http.client.ClientProtocolException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tianjs.http.HttpExcuteService;
import com.tianjs.http.HttpResult;
import com.tianjs.wechat.WechatUtilsService;

import io.github.xdiamond.client.XDiamondConfig;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service
public class WechatUtilsServiceImpl implements WechatUtilsService {
	
	@Autowired
	private HttpExcuteService httpExcuteService;

	@Autowired
	private XDiamondConfig xDiamondConfig; 
	
	@Override
	public String getAccessToken() {
		String appId = xDiamondConfig.getProperty("wx.appid");
		String secret = xDiamondConfig.getProperty("wx.secret");
		String accessTokenUrl = xDiamondConfig.getProperty("wx.accessTokenUrl");
		accessTokenUrl = accessTokenUrl.replaceAll("APPID",appId);
		accessTokenUrl = accessTokenUrl.replaceAll("APPSECRET",secret);
		System.out.println(accessTokenUrl);
		String result = "";
		try {
			result = httpExcuteService.doGet(accessTokenUrl);
		} catch (IOException e) {
			log.error("【获取access token 失败】");
		}
		return result;
	}

	@Override
	public String getTemplateList() {
		String accessToken = "15_0P8hKlItAcHT2hFpZJs_mP_7IxM7THJdmjrhFMVu6Q-M1GVLIIrhRVb_l9K2UfMRrODxkeSrla2N2vnrnWU7xNVAuZd82Ljps8rLWVsuQuhMyF_rPpsQoK6IuZmfwwrSTvl43EArMBeN6aoPJOAhACAQVH";
		String templateUrl = xDiamondConfig.getProperty("wx.getAllPrivateTemplate");
		templateUrl = templateUrl.replaceAll("ACCESS_TOKEN", accessToken);
		String result = "";
		try {
			result = httpExcuteService.doGet(templateUrl);
		} catch (IOException e) {
			log.error("【获取消息模板列表失败】");
		}
		return result;
	}
	
	public void sendMsg() {
		String accessToken = "15_0P8hKlItAcHT2hFpZJs_mP_7IxM7THJdmjrhFMVu6Q-M1GVLIIrhRVb_l9K2UfMRrODxkeSrla2N2vnrnWU7xNVAuZd82Ljps8rLWVsuQuhMyF_rPpsQoK6IuZmfwwrSTvl43EArMBeN6aoPJOAhACAQVH";
		String sendMsgUrl = xDiamondConfig.getProperty("wx.sendTemplate");
		sendMsgUrl = sendMsgUrl.replaceAll("ACCESS_TOKEN", accessToken);
		String json = "{\r\n" + 
				"	\"touser\": \"oyIyjw04p6JejtXzedEX_jzFp4lQ\",\r\n" + 
				"	\"template_id\": \"mIb5OLqtaEIuBZZpJBx4cj_pBsWCwx50cP2zx3pJ_1A\",\r\n" + 
				"	\"url\": \"\",\r\n" + 
				"	\"miniprogram\": {\r\n" + 
				"	},\r\n" + 
				"	\"data\": {\r\n" + 
				"		\"first\": {\r\n" + 
				"			\"value\": \"鑫聚天利1号\",\r\n" + 
				"			\"color\": \"#173177\"\r\n" + 
				"		},\r\n" + 
				"		\"keyword1\": {\r\n" + 
				"			\"value\": \"5.0\",\r\n" + 
				"			\"color\": \"#173177\"\r\n" + 
				"		},\r\n" + 
				"		\"keyword2\": {\r\n" + 
				"			\"value\": \"189\",\r\n" + 
				"			\"color\": \"#173177\"\r\n" + 
				"		},\r\n" + 
				"		\"keyword3\": {\r\n" + 
				"			\"value\": \"一次性到期还本付息\",\r\n" + 
				"			\"color\": \"#173177\"\r\n" + 
				"		},\r\n" + 
				"		\"remark\": {\r\n" + 
				"			\"value\": \"欢迎再次购买！\",\r\n" + 
				"			\"color\": \"#173177\"\r\n" + 
				"		}\r\n" + 
				"	}\r\n" + 
				"}";
		try {
			HttpResult result = httpExcuteService.doPostJson(sendMsgUrl, json);
			System.out.println(result.toString());
		} catch (ClientProtocolException e) {
			log.error("【模板消息发送错误】",e);
		} catch (IOException e) {
			log.error("【模板消息发送错误】",e);
		}
	}

}
